package pers.zhang.controller;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import pers.zhang.entity.WeChatBeen.WcUser;
import pers.zhang.service.WeChatUserService.WcUserService;
import pers.zhang.util.MongoUtil;
import pers.zhang.util.WeXinUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @Classname TestController
 * @Description TODO
 * @Date 2019/1/11 14:12
 * @Created by zhangkai
 */
@Controller
public class TestController {
    @Autowired
    WcUserService wcUserService;
    @RequestMapping("/main")
    public  String main()
    {
        return "jsp/index.jsp";
    }
    @RequestMapping("/select")
    public  String select(ModelMap model)
    {
//        WcUser wcUser=(WcUser)EntityFactory.getInstanceFactory("user");
        WcUser wcUser=new WcUser();
        wcUser.setuId("001");
        WcUser  wcUser1=wcUserService.findone(wcUser);
        if (wcUser1!=null)
        {
            System.out.println(wcUser1.toString());
            model.addAttribute("user",wcUser1);

        }
        else {
            model.addAttribute("message","数据为空");
        }
        return "jsp/index.jsp";
    }
    @RequestMapping("/getPhoto")
     public  void getPhoto(HttpServletResponse response) {
        MongoClient con=MongoUtil.getMongoClient();
        DB db=con.getDB("testMongo");
        String newFileName = "1-java-image";
        GridFS gfsPhoto = new GridFS(db, "photo");
        GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
        System.out.println(imageForOutput);
        OutputStream outputStream=null;
        try {
        response.setContentType("multipart/form-data");
        outputStream = new BufferedOutputStream(response.getOutputStream());
        imageForOutput.writeTo(outputStream); //output to new file
        outputStream.flush();
        } catch (IOException e) {
        e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

    @RequestMapping("/wexinlogin")
    public  String wexinlogin() {
        String backUrl="https://cnkingz.goho.co/callBack";
        String  url= null;
        try {
            url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                    "appid=" +WeXinUtil.APPID+
                    "&redirect_uri="+URLEncoder.encode(backUrl,"UTF-8")+
                    "&response_type=code" +
                    "&scope=snsapi_userinfo" +
                    "&state=STATE#wechat_redirect";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("backUrl:"+backUrl);
        System.out.println("Url:"+url);
        return "redirect:"+url;
    }
    @RequestMapping("/callBack")
    public  String callBack(HttpServletRequest httpRequest,ModelMap model)
    {
        String code=httpRequest.getParameter("code");
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?" +
                "appid="+WeXinUtil.APPID+
                "secret="+WeXinUtil.APPSECRET+
                "&code="+code+
                "&grant_type=authorization_code";
        try {
            JSONObject jsonObject=WeXinUtil.doGetJson(url);
            String OpenID=jsonObject.getString("openid");
            String token=jsonObject.getString("access_token");
            String infoUrl="https://api.weixin.qq.com/sns/userinfo?" +
                    "access_token=" +token+
                    "&openid=" +OpenID+
                    "&lang=zh_CN";
            JSONObject info=WeXinUtil.doGetJson(infoUrl);
           model.addAttribute("nickname",info.getJSONObject("nickname")) ;
            model.addAttribute("city",info.getJSONObject("city")) ;
            model.addAttribute("headimgurl",info.getJSONObject("headimgurl")) ;
            info.getJSONObject("city");
            info.getJSONObject("headimgurl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "jsp/index.jsp";
    }



}
